package org.seeplnframework.skyeye.common.util;

import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodStatus;
import lombok.extern.slf4j.Slf4j;
import org.seeplnframework.skyeye.common.constants.CommonConst;
import org.springframework.cloud.kubernetes.PodUtils;

import javax.annotation.PostConstruct;
import java.io.*;
import java.util.Objects;

@Slf4j
public class K8sUtils {

    public static String IP;

    public static Integer PORT;

    public static String POD_NAME;

    public static String HOST_IP;

    public static String CONTAINER_NAME;
    

    @PostConstruct
    public void init() {
        IP = getSelfIp();
        PORT = getSelfPort();
        POD_NAME = getPodName();
        HOST_IP = getHostIp();
        CONTAINER_NAME = getContainerName();
    }

    public static String getPodName() {
        if (CONTAINER_NAME != null) {
            return CONTAINER_NAME;
        }
        String env = SpringUtils.getProperty("spring.profiles.active");
        if (CommonConst.ENV_DEV.equals(env)) {
            return null;
        }
        if (CommonConst.ENV_K8S.equals(env)) {
            PodUtils podUtils = getPodUtils();
            Pod pod = podUtils.currentPod().get();
            return pod.getMetadata() == null ? null : pod.getMetadata().getName();
        }
        return null;
    }

    public static String getSelfIp() {
        if (IP != null) {
            return IP;
        }
        String ip = "";
        String env = SpringUtils.getProperty("spring.profiles.active");
        if (CommonConst.ENV_DEV.equals(env)) {
            ip = SpringUtils.getProperty("spring.cloud.client.ip-address");
        } else if (CommonConst.ENV_K8S.equals(env)) {
            PodUtils podUtils = getPodUtils();
            PodStatus podStatus = podUtils.currentPod().get().getStatus();
            if (podStatus != null) {
                ip = podStatus.getPodIP();
            } else {
                ip = "0.0.0.0";
            }
        }
        return ip;
    }

    public static String getHostIp() {
        if (HOST_IP != null) {
            return HOST_IP;
        }
        String ip = "";
        String env = SpringUtils.getProperty("spring.profiles.active");
        if (CommonConst.ENV_DEV.equals(env)) {
            ip = SpringUtils.getProperty("spring.cloud.client.ip-address");
        } else if (CommonConst.ENV_K8S.equals(env)) {
            PodUtils podUtils = getPodUtils();
            PodStatus podStatus = podUtils.currentPod().get().getStatus();
            if (podStatus != null) {
                ip = podStatus.getHostIP();
            } else {
                ip = "0.0.0.0";
            }
        }
        return ip;
    }

    public static String getContainerName() {
        if (CONTAINER_NAME != null) {
            return CONTAINER_NAME;
        }
        String env = SpringUtils.getProperty("spring.profiles.active");
        if (CommonConst.ENV_DEV.equals(env)) {
            return SpringUtils.getProperty("spring.application.name");
        } else if (CommonConst.ENV_K8S.equals(env)) {
            PodUtils podUtils = getPodUtils();
            Pod pod = podUtils.currentPod().get();
            return pod.getSpec() == null ? SpringUtils.getProperty("spring.application.name") : pod.getSpec().getContainers().get(0).getName();
        }
        return SpringUtils.getProperty("spring.application.name");
    }

    public static Integer getSelfPort() {
        if (PORT != null) {
            return PORT;
        }
        String port = SpringUtils.getProperty("server.port");
        if (port == null) {
            return null;
        }
        return Integer.parseInt(port);
    }


    private static String getNameSpace() {
        try {
            InputStream inputStream = new FileInputStream("/var/run/secrets/kubernetes.io/serviceaccount/namespace");
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String namespace = reader.readLine();
            return namespace.trim();
        } catch (IOException e) {
            log.error("读取namespace失败!!!!!!!!!!!!!!!!!!");
            return null;
        }
    }
    
    private static PodUtils getPodUtils() {
        PodUtils podUtils = SpringUtils.getBean(PodUtils.class);
        if (Objects.isNull(podUtils)) {
            throw new IllegalStateException("podUitls not found");
        }
        return podUtils;
    }
}
